﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using ClassLibrary1;
using System.Net;

namespace Checkers
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        Game _checkers;
        bool _selected;
        bool _done;

        TextBox _ipAddrTextBox;
        Button _connectButton;
        CheckBox _serverSelector;
        CheckBox _onLineSelector;
        TextBlock _turnDisplay;

        public MainWindow()
        {
            InitializeComponent();

            for (int i = 0; i < 8; i++)
            {
                boardGrid.ColumnDefinitions.Add(new ColumnDefinition());
                boardGrid.RowDefinitions.Add(new RowDefinition());
            }

            for (int i = 0; i < 3; i++)
            {
                RowDefinition rd = new RowDefinition();
                rd.Height = new GridLength(30);
                boardGrid.RowDefinitions.Add(rd);
            }

            bool isBlackTile = false;
            for (int i = 0; i < 8; i++)
            {
                for (int j = 0; j < 8; j++)
                {
                    Label l = new Label();
                    l.MouseUp += new MouseButtonEventHandler(label_MouseUp);
                    l.Content = i + "" + j;
                    l.Name = "c" + i + "r" + j;
                    if (isBlackTile)
                    {
                        l.Background = new SolidColorBrush(Colors.Black);
                        l.Foreground = new SolidColorBrush(Colors.White);
                    }
                    else
                    {
                        l.Background = new SolidColorBrush(Colors.DarkRed);
                        l.Foreground = new SolidColorBrush(Colors.White);
                    }
                    isBlackTile = !isBlackTile;

                    boardGrid.Children.Add(l);
                    Grid.SetRow(l, i);
                    Grid.SetColumn(l, j);
                }
                isBlackTile = !isBlackTile;
            }

            _ipAddrTextBox = new TextBox();
            _ipAddrTextBox.Text = "192.168.1.45";
            SetUiElement(_ipAddrTextBox, 9, 0, 5);

            _connectButton = new Button();
            _connectButton.Click += new RoutedEventHandler(_connectButton_Click);
            _connectButton.Content = "START";
            SetUiElement(_connectButton, 9, 6, 2);

            _serverSelector = new CheckBox();
            _serverSelector.Content = "Is Server";
            SetUiElement(_serverSelector, 10, 0, 2);

            _onLineSelector = new CheckBox();
            _onLineSelector.Content = "Online play";
            SetUiElement(_onLineSelector, 10, 3, 2);

            _turnDisplay = new TextBlock();
            _turnDisplay.Text = "RED's turn";
            SetUiElement(_turnDisplay, 8, 3, 10);
        }



        public void SetUiElement(UIElement uiElement, int row, int col, int span)
        {
            boardGrid.Children.Add(uiElement);
            Grid.SetRow(uiElement, row);
            Grid.SetColumn(uiElement, col);
            Grid.SetColumnSpan(uiElement, span);
        }

        void _connectButton_Click(object sender, RoutedEventArgs e)
        {
            _checkers = new Game();

            _onLineSelector.IsEnabled = false;
            _serverSelector.IsEnabled = false;
            _ipAddrTextBox.IsEnabled = false;
            _connectButton.IsEnabled = false;

            byte[,] board = _checkers.getBoard();
            RenderBoard(board);

            if ((bool)_onLineSelector.IsChecked)
            {
                _checkers.IsNetworked = true;
                IPAddress tmp;
                IPAddress.TryParse(_ipAddrTextBox.Text, out tmp);

                if (tmp != null)
                {
                    Game.IsServer = (bool)_serverSelector.IsChecked;
                    Game.ServerIP = _ipAddrTextBox.Text;
                    
                    _checkers.connect(RenderBoard);
                }
                else
                {
                    MessageBox.Show("The entered IP Address is not valid.");
                }
            }
            else
            {
                _checkers.IsNetworked = false;
            }
        }



        public void RenderBoard(byte[,] board)
        {
            Dispatcher.BeginInvoke(new Action(() =>
            {
                if (board == null)
                {
                    MessageBox.Show("Unable to connect!");
                    return;
                }
                

                foreach (object obj in boardGrid.Children)
                {
                    if (obj is Label)
                    {
                        Label l = (Label)obj;
                        char[] name = l.Name.ToCharArray();
                        int col = Convert.ToInt32(name[1]) - 48;
                        int row = Convert.ToInt32(name[3]) - 48;

                        int element = board[row, col];
                        switch (element)
                        {
                            case Game.black:
                                l.Content = "B";
                                break;
                            case Game.blackKing:
                                l.Content = "BK";
                                break;
                            case Game.blank:
                                l.Content = "";
                                break;
                            case Game.red:
                                l.Content = "R";
                                break;
                            case Game.redKing:
                                l.Content = "RK";
                                break;
                            default:
                                l.Content = "?";
                                break;
                        }
                    }
                }
            }));
        }

        void label_MouseUp(object sender, MouseButtonEventArgs e)
        {
            Label label = (Label)sender;
            char[] name = label.Name.ToCharArray();
            int col = Convert.ToInt32(name[1]) - 48;
            int row = Convert.ToInt32(name[3]) - 48;
            _checkers.ClickSquare(new Tuple<byte, byte>((byte)row, (byte)col), out _selected, out _done, RenderBoard, _checkers.RedsTurn);
            RenderBoard(_checkers.getBoard());

            if (_checkers.RedsTurn)
                _turnDisplay.Text = "RED's turn";
            else
                _turnDisplay.Text = "BLACK's turn";

            if (_done)
                MessageBox.Show("YOU WIN!");
        }
    }
}
